package com.bjgzy.courselive.mvp.presenter;

import android.app.Application;
import android.os.Message;

import com.bjgzy.courselive.mvp.contract.CourseLiveCommentContract;
import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.CourseCommentData;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;

import org.simple.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * @author xubo
 * @date 2018-8-2 11:43:11
 */
@ActivityScope
public class CourseLiveCommentPresenter extends BasePresenter<CourseLiveCommentContract.Model, CourseLiveCommentContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public CourseLiveCommentPresenter(CourseLiveCommentContract.Model model, CourseLiveCommentContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void commentPublish(int internalId, String content, int star) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("resourceId",internalId );
        map.put("userId", SPUtils.getInstance().getString(Constants.USERID));
        map.put("content", content);
        map.put("star", star);
        mModel.commentPublish(map)
                .retryWhen(new RetryWithDelay(2,3))
                .compose(RxUtil.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<CourseCommentData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<CourseCommentData> commonData) {
                        if (commonData.isResult()) {
                            mRootView.showMessage(commonData.getMessage());
                            Message msg = Message.obtain();
                            msg.what = EventBusTags.COMMENT_SUCCESS;
                            EventBus.getDefault().post(msg, RouterHub.COURSELIVE_COURSECOMMENTFRAGMENT);
                            EventBus.getDefault().post(msg, RouterHub.COURSELIVE_COURSEPLAYACTIVITY);

                            mRootView.killMyself();
                        }
                    }
                });
    }
}
